package leetcodecompetition.D344;

import org.junit.jupiter.api.Test;

public class T6419 {
    int sum;
    int n;
    int[] cost;
    public int minIncrements(int n, int[] cost) {
        sum = 0;
        this.n = n;
        this.cost = cost;
        add(1);
        return sum;
    }
    public int add(int f) {
        if (2 * f > n) return cost[f-1];
        int left = add(2*f);
        int right = add(2*f+1);
        int max = Math.max(left, right);
        int min = Math.min(left, right);
        sum += max - min;
        return max + cost[f-1];
    }

    @Test
    public void test() {
        int[] cost = {1, 5, 2, 2, 3, 3, 1};
        System.out.println(minIncrements(7, cost));
    }
}
